package com.itwn.portal.service;

import com.itwn.portal.config.WebMvcConfig;
import com.itwn.portal.entity.Buy;
import com.itwn.portal.entity.User;
import com.itwn.portal.mapper.BuyMapper;
import com.itwn.portal.mapper.UserMapper;

import com.itwn.portal.vo.PasswordVo;
import com.itwn.portal.vo.UserHistoryVo;
import com.itwn.portal.vo.UserRecordVo;
import com.itwn.portal.vo.BookcaseVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Repository
public class UserService {

    @Autowired
    UserMapper userMapper ;
    @Autowired
    HistoryService historyService;
    @Autowired
    BuyMapper buyMapper;

    /**
     *
     * @param username
     * @return
     * 登录
     */
    public User queryUserByName(String username) {
        User user = this.userMapper.selectUserByName(username);
        System.out.println("----------------"+user);
        return user;
    }

    /**
     *
     * @param user
     * @param confirmPassword
     * @return
     *
     * 注册功能
     */
    public int userRegister(User user , String confirmPassword) {
        User userByName = userMapper.selectUserByName(user.getUsername());
        if (userByName == null) {
            user.setNickname(user.getUsername());
            this.userMapper.insert(user) ;
            return 1;
        } else {
            return 2;
        }
    }


    public List<UserRecordVo> queryPersonRecord(Integer uid) {
        return userMapper.selectPersonRecord(uid);
    }
    public List<BookcaseVo> queryBookCase(Integer id) {
        List<BookcaseVo> list = userMapper.selectBookCase(id);
        for (BookcaseVo bookcaseVo : list) {
            UserHistoryVo history = historyService.getHistory(id, bookcaseVo.getResourceId());
            bookcaseVo.setHistory(history);
        }
        return list;

    }

    @CacheEvict(value = "User", allEntries = true)
    public void modifyUserInfo(User user) {
       Integer id = user.getId();
       String nickname = user.getNickname();
       Byte sex= user.getSex();
       Integer age = user.getAge();
        userMapper.updateUserInfo(id,nickname,sex,age);
    }


    public User searchById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    public void modifyPassword(PasswordVo passwordVo) {
        Integer id = passwordVo.getId();
        String newPass = passwordVo.getNewPass();
        userMapper.updatePassword(id,newPass);

    }

    @Transactional
    public Boolean readupdate(User loginUser,Integer sid) {
        Integer readPoint = loginUser.getReadPoint();
        Integer pointTicket = loginUser.getPointTicket();
        if (readPoint>0){
            //扣阅
            userMapper.updateReadPoint(loginUser.getId(),readPoint-1);
        }else if(pointTicket>20){
            //扣点券
            userMapper.updatePointTicket(loginUser.getId(),pointTicket-20);
        }else {
            return false;
        }
        Buy buy = new Buy();
        buy.setUserId(loginUser.getId());
        buy.setSectionId(sid);
        buyMapper.insert(buy);
        return true;
    }

    public Buy queryBuyByUidAndSid(Integer uid, Integer sid) {
        return buyMapper.selectOne(uid,sid);
    }


    public void uploadAvatar(Integer uid, String avatar) {
        userMapper.updateAvater(uid,avatar);
    }
}
